
Page({
  data: {
    input_answer_list: [],  //输入框的数组
    name: '',  //标题名称
    flg: 0,
    colorArr: [//增加选项的默认颜色排序
       '#EE534F',
       '#FF7F50',
       '#FFC928',
       '#66BB6A',
       '#42A5F6',
       '#FF7F50',
       '#AA47BC',
       '#EC407A',
       '#DA70D6',
       '#FFA827',
       '#AA47BC',
       '#EE534F',
       '#42A5F6',
       '#66BB6A',
       '#FFC928',
       '#42A5F6',
       '#5C6BC0',
    ]
 },
 onLoad: function (options) {
  wx.getStorage({
    key:'shuju',
  }).then(res=>{
   let awardsConfig = res.data
    console.log(awardsConfig)
    this.setData({
      input_answer_list:awardsConfig
    })
  })
  

 },
  addAnswer() {
    var that = this, input_answer_list = that.data.input_answer_list, colorArr = that.data.colorArr, obj = {};
    if (input_answer_list.awards.length == 17) {
       wx.showToast({
          title: '选项长度最多17项',
          icon:'none',
          mask:false
       })
       return;
    }
    obj = { id:input_answer_list.awards.length, name: '', color: colorArr[input_answer_list.awards.length],probability:50};
    input_answer_list.awards.push(obj);
    that.setData({
       input_answer_list: input_answer_list
    })
 },
 subAnswer(e) {
  var that = this, index = e.currentTarget.dataset.index, input_answer_list = that.data.input_answer_list, colorArr = that.data.colorArr;
  for (let i in input_answer_list.awards) {
     if (i == index) {
        input_answer_list.awards.splice(i, 1);
        break;
     }
  }
  console.log(input_answer_list)
  for (let x in input_answer_list.awards){
     input_answer_list.awards[x].color = colorArr[x];
  }

  that.setData({
     input_answer_list: input_answer_list
  })
},
checkQuestion(e){
   var val =  e.detail.value,input_answer_list = this.data.input_answer_list
   input_answer_list.option = val
   this.setData({
      input_answer_list:input_answer_list
   })
},
checkAnswer(e) {
  var that = this, val = e.detail.value, index = e.currentTarget.dataset.index, input_answer_list = that.data.input_answer_list
  for (let i in input_answer_list.awards) {
     if (index == i) {
        input_answer_list.awards[i].name = val
     }
  }
  that.setData({
     input_answer_list: input_answer_list
  })
},
saveQA() {
  var that = this,input_answer_list = that.data.input_answer_list
  console.log(input_answer_list)
  if (that.data.input_answer_list.option == '') {
     wx.showToast({
        title: '名称不能为空',
        icon: 'none',
        mask: false
     })
  } else {
     for (let y in input_answer_list.awards) {
        if (input_answer_list.awards[y].name == "") {
           wx.showToast({
              title: '选项不能为空',
              icon: 'none',
              mask: false
           })
           return;
        }
     }

     if (input_answer_list.awards.length < 2) {
        wx.showToast({
           title: '选项最少填2个',
           icon: 'none',
           mask: false
        })
        return;
     }

     if (input_answer_list) {
      wx.setStorage({
        data:this.data.input_answer_list,
        key:'shuju',
      })
        wx.showToast({
           title: '保存成功',
           icon: 'success',
           mask: false,
           success: function () {  //提示成功1000毫秒后再重新跳转
            setTimeout(function() {
              wx.reLaunch({
                url:'./turn'
              })
            }, 1000);
          }
        })
        return;
     }
  }
}
})